
#include <gtest/gtest.h>

#include "timestampparser.h"

namespace
{
	using namespace Wishmaster;

	class TestTimestampParser : public ::testing::Test
	{
	public:
		virtual void SetUp()
		{

		}

		virtual void TearDown()
		{

		}

		TimestampParser m_parser;
	};

	TEST_F(TestTimestampParser, DateYear4Digit)
	{
		QString str = "24/10/2011 12:30:51 ";
		m_parser.setPattern("d/M/y h:m:s");

		QDateTime dt = m_parser.parse(str);
		ASSERT_EQ(24, dt.date().day());
		ASSERT_EQ(10, dt.date().month());
		ASSERT_EQ(2011, dt.date().year());

		ASSERT_EQ(12, dt.time().hour());
		ASSERT_EQ(30, dt.time().minute());
		ASSERT_EQ(51, dt.time().second());
	}

	TEST_F(TestTimestampParser, DateYear2Digit)
	{
		QString str = "24/10/11 12:30:51 ";
		m_parser.setPattern("d/M/y h:m:s");

		QDateTime dt = m_parser.parse(str);
		ASSERT_EQ(2011, dt.date().year());
	}

	TEST_F(TestTimestampParser, DateMonthName)
	{
		QString str = "24 Oct 11 12:30:51 ";
		m_parser.setPattern("d N y h:m:s");

		QDateTime dt = m_parser.parse(str);
		ASSERT_EQ(24, dt.date().day());
		ASSERT_EQ(10, dt.date().month());
		ASSERT_EQ(2011, dt.date().year());
	}

	TEST_F(TestTimestampParser, DateWithoutSeconds)
	{
		QString str = "24/10/2011 12:30 ";
		m_parser.setPattern("d/M/y h:m");

		QDateTime dt = m_parser.parse(str);
		ASSERT_EQ(24, dt.date().day());
		ASSERT_EQ(10, dt.date().month());
		ASSERT_EQ(2011, dt.date().year());

		ASSERT_EQ(12, dt.time().hour());
		ASSERT_EQ(30, dt.time().minute());
		ASSERT_EQ(0, dt.time().second());
	}

	TEST_F(TestTimestampParser, TirechFormat)
	{
		QString str = QString::fromUtf8("&nbsp;&nbsp;Сбт 19 Мар 2011 09:44:36");
		m_parser.setPattern("d N y h:m:s");

		QDateTime dt = m_parser.parse(str);
		ASSERT_EQ(19, dt.date().day());
		ASSERT_EQ(3, dt.date().month());
		ASSERT_EQ(2011, dt.date().year());

		ASSERT_EQ(9, dt.time().hour());
		ASSERT_EQ(44, dt.time().minute());
		ASSERT_EQ(36, dt.time().second());
	}

	TEST_F(TestTimestampParser, IichanruFormat)
	{
		QString str = QString::fromUtf8("   Сб 19 марта 2011 14:45:30");
		m_parser.setPattern("d N y h:m:s");

		QDateTime dt = m_parser.parse(str);
		ASSERT_EQ(19, dt.date().day());
		ASSERT_EQ(3, dt.date().month());
		ASSERT_EQ(2011, dt.date().year());

		ASSERT_EQ(14, dt.time().hour());
		ASSERT_EQ(45, dt.time().minute());
		ASSERT_EQ(30, dt.time().second());
	}
}
